
package biblio.dao;


import biblio.domain.Hire;
import biblio.exception.NoSuchEntityException;
import java.io.Serializable;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


/**
 * Default implementation of HireDAO.
 * @author mephisto
 */
public class HireDAOImpl extends HibernateDaoSupport implements HireDAO
{

    public void create(Hire hire)
    {
        //attention persist throws exception!!
        //org.hibernate.PersistentObjectException:
        //detached entity passed to persist: biblio.domain.Category
        getHibernateTemplate().merge(hire);
    }

    /**
     * @throws DataAccessException - in case of Hibernate errors
     */
    public List<Hire> find(Serializable lastName) throws NoSuchEntityException
    {
        List<Hire> res = getHibernateTemplate().findByNamedParam(
                "from Hire join Reader r where r.lastName = :lastName", "lastName", lastName);
        if (res.isEmpty())
            throw new NoSuchEntityException("No hire under lastName: " + lastName);
        return res;
    }

    /**
     * @throws DataAccessException - in case of Hibernate errors
     */
    public void update(Hire hire) throws NoSuchEntityException
    {
        log.error("Hire for update: " + hire);
        System.out.println("XXX-Hire for update: " + hire);
        getHibernateTemplate().update(hire);
    }

    /**
     * @throws DataAccessException - in case of Hibernate errors
     */
    public void delete(Hire hire)
    {
        getHibernateTemplate().delete(hire);
    }

    /**
     * @throws DataAccessException - in case of Hibernate errors
     */
    public List<Hire> selectAll()
    {
        return getHibernateTemplate().find("from Hire");
    }

    private static Logger log = Logger.getLogger(HireDAOImpl.class);
}
